package com.orchard.xpz.service.grandPlot.impl;

import com.github.pagehelper.PageHelper;
import com.orchard.xpz.dao.GardenPlotDao;
import com.orchard.xpz.entity.GardenPlot.GardenPlot;
import com.orchard.xpz.service.grandPlot.GardenPlotService;
import com.orchard.xpz.utils.PageResultData;
import com.orchard.xpz.utils.UserRuntime;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @TaskCode:
 * @description:
 * @author: minshuai.chen
 * @create: 2022/4/19 14:30
 **/
@Service
public class GardenPlotServiceImpl implements GardenPlotService {

    @Autowired
    private GardenPlotDao gardenPlotDao;

    @Override
    public PageResultData<GardenPlot> getPageList(GardenPlot gardenPlot) {
        PageHelper.startPage(gardenPlot.getPageNum(),gardenPlot.getPageSize());
        List<GardenPlot> list = gardenPlotDao.getList(gardenPlot);
        PageResultData<GardenPlot> pageResultData = new PageResultData<>(list, gardenPlot.getPageNum(), gardenPlot.getPageSize());
        return pageResultData;
    }

    @Override
    public boolean saveOrUpdate(GardenPlot gardenPlot) {
        if (ObjectUtils.isEmpty(gardenPlot.getGardenId())){
            gardenPlot.setCreateName(UserRuntime.getUser().getUserName());
            gardenPlot.setCreateTime(LocalDate.now());
            int i = gardenPlotDao.insertSelective(gardenPlot);
            return i > 0? true : false;
        }else {
            gardenPlot.setUpdateTime(LocalDate.now());
            gardenPlot.setUpdateName(UserRuntime.getUser().getUserName());
            int i = gardenPlotDao.updateByPrimaryKeySelective(gardenPlot);
            return i > 0? true : false;
        }
    }

    @Override
    public boolean deltePlot(String id) {
        return gardenPlotDao.deleteByPrimaryKey(id) > 0? true : false;
    }

    @Override
    public List<GardenPlot> getList(GardenPlot gardenPlot) {

        return gardenPlotDao.getList(gardenPlot);
    }
}
